from django.db import models
from terminal.models import Terminal


class Company(models.Model):
    """企业
    """
    # 按照新版营业执照
    fullname = models.CharField(max_length=30, blank=True, null=True, verbose_name='企业全名称')
    unified_social_credit_code = models.CharField(max_length=20, blank=True, null=True, verbose_name='统一社会信用代码')
    category = models.CharField(max_length=30, blank=True, null=True, verbose_name='类型')
    address = models.CharField(max_length=50, blank=True, null=True, verbose_name='住所')
    legal_representative = models.CharField(max_length=20, blank=True, null=True, verbose_name='法定代表人')
    registered_capital = models.CharField(max_length=30, blank=True, null=True, verbose_name='注册资本')
    date_of_found = models.DateField(blank=True, null=True, verbose_name='成立日期')
    date_of_expire = models.DateField(blank=True, null=True, verbose_name='营业期限')
    date_of_issue = models.DateField(blank=True, null=True, verbose_name='登记日期')
    issue_authority = models.CharField(max_length=20, blank=True, null=True, verbose_name='登记机关')
    # 自定义
    name = models.CharField(max_length=50, verbose_name='企业短名称', blank=True, null=True)
    logo_url = models.CharField(max_length=100, blank=True, null=True, verbose_name='企业LOGO地址')
    created_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_on = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'company'
        verbose_name = '企业'
        verbose_name_plural = verbose_name
        ordering = ('-id', )

    def __str__(self):
        return self.fullname


class Vehicle(models.Model):
    """车辆
    """
    VEHICLE_TYPES = [(t, t)for t in ['微型货车', '轻型货车',
                                     '中型货车', '重型货车', '轻型越野车', '中型越野车', '重型越野车', '超重型越野车', '轻型自卸车',
                                     '中型自卸车', '重型自卸车', '矿用自卸车', '半挂牵引车', '全挂牵引车', '箱式汽车', '罐式汽车',
                                     '起重举升车', '仓栅式车', '特种结构车', '专用自卸车', '微型客车', '轻型客车', '中型客车', '大型客车',
                                     '特大型客车', '微型轿车', '普通级轿车', '中级轿车', '中高级轿车', '高级轿车', '轻型半挂车',
                                     '中型半挂车', '重型半挂车', '超重半挂车']]
    USE_CHARACTERS = [(t, t)for t in ['家庭自用车', '非营业客车', '营业客车', '非营业货车', '营业货车', '专用车', '危化车', '冷链车']]

    # 按照行驶证
    license_number = models.CharField(max_length=15, db_index=True, verbose_name='车牌号')
    vin = models.CharField(max_length=20, blank=True, null=True, db_index=True, verbose_name='车辆识别代号:17位')
    color = models.CharField(max_length=5, blank=True, null=True, verbose_name='车辆颜色')
    engine_number = models.CharField(max_length=15, blank=True, null=True, verbose_name='发动机编号')
    use_character = models.CharField(max_length=15, blank=True, null=True, choices=USE_CHARACTERS, verbose_name='使用性质')
    vehicle_type = models.CharField(max_length=15, blank=True, null=True, choices=VEHICLE_TYPES, verbose_name='车辆类型')
    model = models.CharField(max_length=30, blank=True, null=True, verbose_name='品牌型号')
    date_of_register = models.DateField(blank=True, null=True, verbose_name='注册日期')
    date_of_issue = models.DateField(blank=True, null=True, verbose_name='发证日期')
    file_number = models.CharField(max_length=15, blank=True, null=True, verbose_name='档案编号')
    total_weight = models.CharField(max_length=15, blank=True, null=True, verbose_name='总质量')
    grub_weight = models.CharField(max_length=15, blank=True, null=True, verbose_name='整备质量')
    approved_load = models.CharField(max_length=15, blank=True, null=True, verbose_name='核定载重：12760kg')
    approved_personnel_number = models.CharField(max_length=15, blank=True, null=True, verbose_name='核定载人数：2人')
    outside = models.CharField(max_length=15, blank=True, null=True, verbose_name='外观尺寸：4783X1810X1442mm')
    note = models.CharField(max_length=15, blank=True, null=True, verbose_name='备注')
    inspection_record = models.CharField(max_length=20, blank=True, null=True, verbose_name='检验记录：检验有效期至2016年05月鲁L(01)')
    # 自定义
    created_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_on = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.license_number

    class Meta:
        db_table = 'vehicle'
        verbose_name = '车辆'
        verbose_name_plural = verbose_name
        ordering = ('-id', )


class Driver(models.Model):
    """司机
    """
    GenderChoice = (
        ('M', '男'),
        ('F', '女'),
    )
    # 按照驾驶证
    name = models.CharField(max_length=10)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, db_index=True, verbose_name='企业')
    vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE, db_index=True, null=True, blank=True, verbose_name='车辆')
    gender = models.CharField(max_length=4, choices=GenderChoice, default=GenderChoice[0][0], verbose_name='性别')
    phone_number = models.CharField(max_length=15, blank=True, null=True, verbose_name='联系电话')
    id_number = models.CharField(max_length=18, blank=True, null=True, verbose_name='身份证号码')
    qualification_cert_number = models.CharField(max_length=20, blank=True, null=True, verbose_name='从业资格证件号码')
    portrait_url = models.CharField(max_length=100, blank=True, null=True, verbose_name='头像URL')
    date_of_first_issue = models.DateField(blank=True, null=True, verbose_name='初次发证日期')
    date_of_issue = models.DateField(blank=True, null=True, verbose_name='发证日期')
    date_of_expire = models.DateField(blank=True, null=True, verbose_name='过期日期')
    # 自定义
    created_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_on = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'driver'
        verbose_name = '司机'
        verbose_name_plural = verbose_name
        ordering = ('-id', )


class VehicleCompanyTerminal(models.Model):
    """车辆企业终端关系表

    TODO: 是否需要状态字段待定
    """
    vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE, db_index=True)
    company = models.ForeignKey(Company, on_delete=models.CASCADE, db_index=True)
    terminal = models.ForeignKey(Terminal, on_delete=models.CASCADE, null=True, blank=True, db_index=True)
    created_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_on = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'relation_vehicle_company_terminal'
        verbose_name = '车辆企业终端关系表'
        verbose_name_plural = verbose_name
        ordering = ('-id', )
